package crp.services.api.graphql.models;

import crp.core.resource.domain.models.Attachment;
import crp.core.resource.domain.models.ResourceEntity;
import crp.core.resource.domain.models.ResourceSourceType;
import crp.core.shared.LifecycleStatus;
import crp.core.shared.PublicStatus;
import crp.core.shared.PublishStatus;
import crp.core.workflow.domain.models.Approval;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import org.eclipse.microprofile.graphql.Id;
import org.eclipse.microprofile.graphql.Ignore;
import org.eclipse.microprofile.graphql.Type;

import javax.validation.constraints.NotNull;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;

@Type("Resource")
@Builder
@Getter
@AllArgsConstructor
public class ResourcePayload {
    @Id
    @NotNull
    private final String id;
    @NotNull
    private final String name;
    @Ignore
    private String spaceId;
    private Instant startAt;
    private Instant endAt;
    private Duration duration;
    private ResourceSourceType sourceType;
    private Instant syncedAt;
    private PublishStatus publishStatus;
    private PublicStatus publicStatus;
    private LifecycleStatus status;
    @Ignore
    private final Attachment attachment;
    @Ignore
    private final Approval approval;

    public static ResourcePayload of(ResourceEntity entity) {
        return ResourcePayload.builder()
                .id(entity.getId())
                .name(Optional.ofNullable(entity.getAlias()).orElse(entity.getName()))
                .spaceId(entity.getSpaceId())
                .duration(entity.getDuration())
                .startAt(entity.getStartAt())
                .endAt(entity.getEndAt())
                .sourceType(entity.getSourceType())
                .syncedAt(entity.getSyncedAt())
                .publishStatus(entity.getPublishStatus())
                .publicStatus(entity.getPublicStatus())
                .status(entity.getStatus())
                .attachment(entity.getAttachment())
                .approval(entity.getApproval())
                .build();
    }
}
